package com.jmu.edu.IMApplication.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.checkbox.MaterialCheckBox;
import com.google.android.material.imageview.ShapeableImageView;
import com.jmu.edu.IMApplication.R;
import com.jmu.edu.IMApplication.event.NotifyToSendMessageEvent;
import com.jmu.edu.IMApplication.event.SendMessageEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.List;

public class AlbumItemAdapter extends RecyclerView.Adapter<AlbumItemAdapter.MyViewHolder> {
    private List<File> fileList;
    private int selectedIndex = -1;
    private RecyclerView recyclerView;


    public void setFileList(List<File> fileList) {
        this.fileList = fileList;
    }

    public AlbumItemAdapter() {
        EventBus.getDefault().register(this);
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_album, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        File file = fileList.get(position);
        Glide.with(holder.imageView)
                .load(file.getAbsolutePath())
                .into(holder.imageView);
        holder.checkBox.setChecked(selectedIndex == position);


    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    @Override
    public int getItemCount() {
        return fileList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private ShapeableImageView imageView;
        private MaterialCheckBox checkBox;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.item_album_image);
            checkBox = itemView.findViewById(R.id.item_album_checkbox);
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int position = getAdapterPosition();
                    // 这里代表此时视图位置还未刷新
                    if (position == RecyclerView.NO_POSITION) {
                        return;
                    }
                    if (recyclerView.isComputingLayout()) {
                        recyclerView.post(new Runnable() {
                            @Override
                            public void run() {
                                onCheckStateChange(itemView, position, isChecked);
                            }
                        });
                    } else {
                        onCheckStateChange(itemView, position, isChecked);
                    }

                }
            });
        }
    }

    public File getSelectedFile() {
        if (selectedIndex != -1) {
            return fileList.get(selectedIndex);
        } else {
            return null;
        }
    }

    private void onCheckStateChange(View itemView, int position, boolean isChecked) {
        if (isChecked) {
            //选中当前，则要通知上一个被选中的item进行修改
            itemView.setAlpha(0.5f);
            int lastPosition = selectedIndex;
            selectedIndex = position;
            //重新加载上一个选中的item
            if (lastPosition != -1 && lastPosition != position) {
                notifyItemChanged(lastPosition);
            }

        } else {
            itemView.setAlpha(1);
            if (position == selectedIndex) {
                selectedIndex = -1;
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void onNotifyToSendMessageEvent(NotifyToSendMessageEvent event) {
        //如果要发送的信息是图片信息
        if (event.getMessageType() == NotifyToSendMessageEvent.IMAGE_TYPE) {
            SendMessageEvent sendMessageEvent = new SendMessageEvent();
            sendMessageEvent.setMessageType(event.getMessageType());
            sendMessageEvent.setMessageContent(getSelectedFile());
            EventBus.getDefault().post(sendMessageEvent);
        }
    }
}
